package com.guodi.map.controller;

import com.guodi.common.utils.SoeUtil;
import com.guodi.common.utils.WordUtil;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.core.minio.util.MinioUtil;
import com.guodi.core.tool.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @描述：分析相关接口控制类
 * @作者: 邹艺
 * @日期: 2020-05-29
 */
@Validated
@RestController
@RequestMapping("/MapAnalysis")
@Api(value = "API - MapAnalysisController", tags = "分析相关接口控制类")
public class MapAnalysisController extends BaseController {

    /**
     * @描述：合规分析-导出报告
     * @作者：邹艺
     * @时间：2020/05/29 14:38
     */
    @PostMapping("/exportHgfxReport")
    @ApiOperation(value = " 导出合规分析PDF报告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "soeUrl", value = "SOE接口地址", required = true,dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "exportObj", value = "接口参数Json字符串", required = true,dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "xmbh", value = "项目编号", required = false,dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "ReportType", value = "报告类型，默认：MedicalProjectReport", required = false,dataType = "string", paramType = "query")
    })
    public R exportHgfxReport(String soeUrl, String exportObj, String xmbh, String ReportType) {
        Map<String, String> param = new HashMap<String, String>();
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        String fileName = "合规分析报告_" + (StringUtils.isBlank(xmbh) ? System.currentTimeMillis() : xmbh);
        File tempDocFile = null;
        File tempPdfFile = null;
        try {
            param.put("InformationJson", exportObj);
            if(StringUtils.isBlank(ReportType)) {
                param.put("ReportType", "MedicalProjectReport");
            } else {
                param.put("ReportType", ReportType);
            }
            param.put("f", "word");
            byte[] is = SoeUtil.callRestSoeURLServiceInputStream(soeUrl, param);
            // doc文件存储绝对路径
            tempDocFile = File.createTempFile(fileName, ".doc");

            fos = new FileOutputStream(tempDocFile);
            bos = new BufferedOutputStream(fos);
            bos.write(is);

            tempPdfFile = File.createTempFile(fileName, ".pdf");

            WordUtil.doc2pdf(tempDocFile, tempPdfFile);
            String fileId = MinioUtil.uploadFile(new FileInputStream(tempPdfFile),fileName + ".pdf");
            return R.data(fileId);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(tempDocFile != null && tempDocFile.exists()){
                tempDocFile.delete();
            }
            if(tempPdfFile != null && tempPdfFile.exists()){
                tempPdfFile.delete();
            }
        }
    }
}
